package com.xyy.saas.payment.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.api.PaymentSettleApi;
import com.xyy.saas.payment.cores.bo.OrderSettleResultBo;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.param.ProfitSharingParam;
import com.xyy.saas.payment.cores.param.ProfitSharingQueryParam;
import com.xyy.saas.payment.cores.vo.*;
import com.xyy.saas.payment.dao.model.ProfitSharing;
import com.xyy.saas.payment.payment.core.service.ProfitSharingService;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangtianqi
 * @date 2019/11/22 17:26
 */
@Service(version = "1.0.0", timeout = 2 * 60 * 1000)
public class PaymentSettleApiImpl implements PaymentSettleApi {
    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(PaymentSettleApiImpl.class);

    @Resource
    private ProfitSharingService profitSharingService;

    /**
     * 订单发起清分接口
     *
     * @param orderClearingVo
     * @param sign
     * @return 只需要根据返回值消息号判断是否成功
     */
    @Override
    public ResultVO orderClearing(OrderClearingVo orderClearingVo, String sign) {
        LOGGER.info("PaymentSettleApiImpl#orderClearing begin OrderClearingVo:{}", JSON.toJSONString(orderClearingVo));
        return ResultVO.createError(ResultCodeEnum.UN_SUPPORT_METHOD);
    }

    /**
     * 订单退款发起退清分接口
     *
     * @param orderRefundClearingVo
     * @param sign
     * @return 只需要根据返回值消息号判断是否成功
     */
    @Override
    public ResultVO orderRefundClearing(OrderRefundClearingVo orderRefundClearingVo, String sign) {
        LOGGER.info("PaymentSettleApiImpl#orderRefundClearing begin OrderRefundClearingVo:{}", JSON.toJSONString(orderRefundClearingVo));
        return ResultVO.createError(ResultCodeEnum.UN_SUPPORT_METHOD);
    }

    /**
     * 设置订单结算时间接口
     *
     * @param orderSettleVo
     * @param sign
     * @return
     */
    @Override
    public ResultVO<OrderSettleResultBo> orderSettle(OrderSettleVo orderSettleVo, String sign) {
        LOGGER.info("PaymentSettleApiImpl#orderSettle begin OrderSettleVo:{}", JSON.toJSONString(orderSettleVo));
        return ResultVO.createError(ResultCodeEnum.UN_SUPPORT_METHOD);
    }

    /**
     * 结算/分润
     *
     * @param profitsharingParam
     * @param sign
     * @return
     */
    @Override
    public ResultVO<ProfitSharingVo> profitSharing(ProfitSharingParam profitsharingParam, String sign) {
        try {
            return ResultVO.createSuccess(profitSharingService.profitSharing(profitsharingParam));
        }
        catch(PaymentException ex) {
            LOGGER.error(ex.getErrMsg() ,ex);
            return ResultVO.createError(ex.getErrCode(), ex.getErrMsg());
        }
        catch(Exception ex) {
            LOGGER.error(ex.getMessage() ,ex);
            return ResultVO.createError(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    @Override
    public ResultVO<Boolean> xydCreditPaySettle(String payNo) {
        LOGGER.info("###PaymentSettleApiImpl xydCreditPaySettle exec PayNo:{}", payNo);

        if (StringUtils.isBlank(payNo)) {
            return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
        }

        try {
            profitSharingService.xydCreditPaySettle(payNo);

            return ResultVO.createSuccess(Boolean.TRUE);
        } catch (Exception e) {
            LOGGER.error("###PaymentSettleApiImpl xydCreditPaySettle error, {}-{}", payNo, e.getMessage(), e);
            return ResultVO.createError(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    @Override
    public void retryAllProfit(Long minId,Integer status) {
        profitSharingService.retryAllProfit(minId,status);
    }
    @Override
    public ResultVO pinganCreditProfitsharing(List<String> payNos) {
        try {
            if (CollectionUtils.isEmpty(payNos)) {
                return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
            }
            profitSharingService.pinganCreditProfitsharing(payNos);
        } catch (Exception e) {
            LOGGER.error("pinganCreditProfitsharing error {}",payNos,e);
            return ResultVO.createError(e.getMessage());
        }
        return ResultVO.createSuccess();
    }

    @Override
    public ResultVO<ProfitSharingVo> queryProfitSharingNoByBusinessOrderNo(ProfitSharingQueryParam queryParam, String sign) {
        if(null == queryParam || null == queryParam.getBusinessOrderType() || StringUtils.isBlank(queryParam.getBusinessOrderNo())) {
            return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
        }
        ProfitSharing profitSharing = profitSharingService.queryByBusinessOrderNoAndType(queryParam.getBusinessOrderType().getType(), queryParam.getBusinessOrderNo());
        ProfitSharingVo result = new ProfitSharingVo();
        if(null != profitSharing) {
            result.setBusinessOrderNo(queryParam.getBusinessOrderNo());
            result.setProfitSharingNo(profitSharing.getProfitSharingNo());
        }
        return ResultVO.createSuccess(result);
    }

    @Override
    public void retryProfitSharing(List<String> list) {
        try {
            if(CollectionUtils.isEmpty(list)) {
                return ;
            }
            profitSharingService.retryProfitSharing(list);
        } catch (Exception e) {
            LOGGER.error("updateProfitSharingStatus ", e);

        }

    }
}
